package com.ruoyi.project.WebMoudle.WxCitys.controller;

import java.util.List;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.constant.WxConstant;
import com.ruoyi.framework.web.domain.Ztree;
import com.ruoyi.project.WebMoudle.WxShop.service.IWxShopService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.WebMoudle.WxCitys.domain.WxCitys;
import com.ruoyi.project.WebMoudle.WxCitys.service.IWxCitysService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 城区设定Controller
 *
 * @author ruoyi
 * @date 2020-06-18
 */
@Controller
@RequestMapping("/WxCitys/WxCitys")
public class WxCitysController extends BaseController
{
    private String prefix = "WxCitys/WxCitys";

    @Autowired
    private IWxCitysService wxCitysService;


    @RequiresPermissions("WxCitys:WxCitys:view")
    @GetMapping()
    public String WxCitys()
    {
        return prefix + "/WxCitys";
    }

    /**
     * 查询城区设定列表
     */
    @RequiresPermissions("WxCitys:WxCitys:list")
    @PostMapping("/list")
    @ResponseBody
    public List<WxCitys> list(WxCitys wxCitys)
    {
      List<WxCitys> list =  wxCitysService.selectWxCitysList(wxCitys);
        for (WxCitys citys : list) {
            Integer count =wxCitysService.selectShopCount(citys.getCityId());
            citys.setCityCount(count);
        }
        return list;
    }

//    /**
//     * 导出城区设定列表
//     */
//    @RequiresPermissions("WxCitys:WxCitys:export")
//    @Log(title = "城区设定", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    @ResponseBody
//    public AjaxResult export(WxCitys wxCitys)
//    {
//        List<WxCitys> list = wxCitysService.selectWxCitysList(wxCitys);
//        ExcelUtil<WxCitys> util = new ExcelUtil<WxCitys>(WxCitys.class);
//        return util.exportExcel(list, "WxCitys");
//    }

    /**
     * 新增城区设定
     */
    @GetMapping("/add/{parentId}")
    public String add(@PathVariable("parentId") Long parentId,ModelMap mmap)
    {
        mmap.put("wxCity", wxCitysService.selectWxCitysById(parentId));
        return prefix + "/add";
    }

    /**
     * 新增保存城区设定
     */
    @RequiresPermissions("WxCitys:WxCitys:add")
    @Log(title = "城区设定", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WxCitys wxCitys)
    {
        if (WxConstant.CITY_NAME_NOT_UNIQUE.equals(wxCitysService.checkNameUnique(wxCitys)))
        {
            return error("新增城区'" + wxCitys.getCityName() + "'失败，名称已存在");
        }
        return toAjax(wxCitysService.insertWxCitys(wxCitys));
    }

    /**
     * 校验城区名称是否重复
     */
    @PostMapping("/checkNameUnique")
    @ResponseBody
    public String checkGmclaNameUnique(WxCitys wxCitys)
    {
        return wxCitysService.checkNameUnique(wxCitys);
    }

    /**
     * 选择班级树
     */
    @GetMapping("/selectCitysTree/{cityId}")
    public String selectGmClassTree(@PathVariable("cityId") Long cityId, ModelMap mmap)
    {
        mmap.put("wxCitys", wxCitysService.selectWxCitysById(cityId));
        return prefix + "/tree";
    }

    /**
     * 加载城区列表树
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = wxCitysService.selectWxCitysTree(new WxCitys());
        return ztrees;
    }

    /**
     * 修改城区设定
     */
    @GetMapping("/edit/{cityId}")
    public String edit(@PathVariable("cityId") Long cityId, ModelMap mmap)
    {
        WxCitys wxCitys = wxCitysService.selectWxCitysById(cityId);
        mmap.put("wxCitys", wxCitys);
        return prefix + "/edit";
    }

    @GetMapping("/selectOneCity/{cityId}")
    @ResponseBody
    public String selectOneCity(@PathVariable("cityId") Long cityId)
    {
        WxCitys wxCitys = wxCitysService.selectWxCitysById(cityId);
       return wxCitys.getCityName();
    }

    /**
     * 修改保存城区设定
     */
    @RequiresPermissions("WxCitys:WxCitys:edit")
    @Log(title = "城区设定", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WxCitys wxCitys)
    {
        if (WxConstant.CITY_NAME_NOT_UNIQUE.equals(wxCitysService.checkNameUnique(wxCitys)))
        {
            return error("修改城区'" + wxCitys.getCityName() + "'失败，城区名称已存在");
        }
        else if(wxCitys.getParentId().equals(wxCitys.getCityId()))
        {
            return error("修改城区'" + wxCitys.getCityName() + "'失败，上级城区不能是自己");
        }
        return toAjax(wxCitysService.updateWxCitys(wxCitys));
    }

    /**
     * 删除城区设定
     */
    @RequiresPermissions("WxCitys:WxCitys:remove")
    @Log(title = "城区设定", businessType = BusinessType.DELETE)
    @GetMapping( "/remove/{cityId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("cityId") Long cityId)
    {
        if (wxCitysService.selectCitysCount(cityId)>0){
            return AjaxResult.warn("存在子城区,不允许删除");
        }
        return toAjax(wxCitysService.deleteWxCitysById(cityId));
    }
}
